package com.vortex.cloud.sample.controller;

import com.vortex.cloud.sample.domain.TaskUser;
import com.vortex.cloud.sample.dto.TaskUserDTO;
import com.vortex.cloud.sample.dto.TaskUserQueryDTO;
import com.vortex.cloud.sample.service.TaskUserService;
import com.vortex.cloud.sample.vo.TaskUserTreeDataVo;
import com.vortex.cloud.sample.vo.TaskUserVo;
import com.vortex.cloud.vfs.lite.base.dto.DataStoreDTO;
import com.vortex.cloud.vfs.lite.base.dto.RestResultDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import javafx.concurrent.Task;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-10-13 周三
 * @time 14:31
 * @description
 */
@Tag(name = "Task用户管理")
@Validated
@CrossOrigin
@RestController
@RequestMapping("cloud/sample/api/v101/task")
public class TaskUserController {

    @Resource
    private TaskUserService taskUserService;

    @Operation(summary = "保存")
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public RestResultDTO<String> save(@Parameter(description = "用户ID") @RequestHeader String tenantId,
                                      @RequestBody TaskUserDTO dto) {
        return taskUserService.save(tenantId, dto);
    }

    @Operation(summary = "修改")
    @PostMapping(value = "/update")
    public RestResultDTO<String> update(@Parameter(description = "用户ID") @RequestHeader String id,
                                        @RequestBody TaskUserDTO dto) {
        return taskUserService.update(id, dto);
    }

    @Operation(summary = "删除")
    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public RestResultDTO<Void> delete(@RequestParam Set<String> ids) {
        taskUserService.delete(ids);
        return RestResultDTO.newSuccess(null, "删除成功");
    }

    @Operation(summary = "分页查询")
    @RequestMapping(value = "/page", method = {RequestMethod.POST})
    public RestResultDTO<DataStoreDTO<TaskUserVo>> page(@ParameterObject @PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                        @Parameter(description = "租户ID") @RequestHeader String tenantId,
                                                        @RequestBody TaskUserQueryDTO queryDTO) {
        return RestResultDTO.newSuccess(taskUserService.page(tenantId, pageable, queryDTO));

    }

    @Operation(summary = "分页查询")
    @PostMapping("/list/page")
    public RestResultDTO<DataStoreDTO<TaskUserVo>> listPage(@ParameterObject @PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                            @RequestBody TaskUserQueryDTO queryDTO) {
        return RestResultDTO.newSuccess(taskUserService.listPage(pageable, queryDTO));
    }


    //树表结构
    @Operation(summary = "树形接口", description = "构造elementUI需要的树表数据")
    @GetMapping(value = "/listTreeData")
    public RestResultDTO listTreeData() {
        List<TaskUserTreeDataVo> ret = new ArrayList<>();
        List<TaskUser> all = taskUserService.list();
        List<TaskUser> first = all.stream()
                .filter(x -> x.getParentId() != null)
                .collect(Collectors.toList());
        for (TaskUser mo : first) {
            ret.add(generateTreeData(mo, all));
//                ret.add(generateTreeData(mo, ret, all));
        }
        return RestResultDTO.newSuccess(ret, "查看成功！");
    }

    /**
     * @Description: 递归方法--把 TaskUser 转换成 TaskUserTreeDataVo
     * @Param: [mo, all]
     * @return: com.vortex.cloud.sample.vo.TaskUserTreeDataVo
     * @Author: 王林
     * @Date: 2021/10/18
     */
    private TaskUserTreeDataVo generateTreeData(TaskUser mo, List<TaskUser> all) {
        TaskUserTreeDataVo node = new TaskUserTreeDataVo();
        BeanUtils.copyProperties(mo, node);
        // 子菜单集合
        List<TaskUser> children = all.stream()
                .filter(x -> x.getParentId() != null && x.getParentId().equals(mo.getTenantId()))
//                .sorted(Comparator.comparing((TaskUser::getSortCode)))
                .collect(Collectors.toList());
        List<TaskUserTreeDataVo> subNodeList = new ArrayList<>();
        // 把子菜单集合添加到 SysMenuTreeData的children 中
        for (TaskUser child : children) {
            subNodeList.add(generateTreeData(child, all));
        }
        node.setChildren(subNodeList);
        return node;
    }
}
